package com.example.purchase.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.purchase.MyApplication;
import com.example.purchase.R;
import com.example.purchase.bean.BaseBean;
import com.example.purchase.bean.MainBean;
import com.example.purchase.utils.Dialog_popWindow;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.List;

public class RecyclerViewAdapter<T> extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>{

    private T beanList;
    private Context context;
    private int type;
    private int size;

    private List<MainBean>  ticketBeanList;

    public RecyclerViewAdapter(T beanList, Context context,int type,int size) {
        this.beanList = beanList;
        this.context = context;
        this.type = type;
        this.size = size;

        switch (type){
            case MyApplication.ORDER_LIST:      //订单列表

                break;
            case MyApplication.LOVE_LIST:     //喜欢列表

                break;
            case MyApplication.ADMIN_LIST:     //票务管理
                if (ticketBeanList == null) ticketBeanList = (List<MainBean>) beanList;
                Log.e("TAG", "onCreateViewHolder: " + ticketBeanList);
                break;
            case MyApplication.ADMIN_ORDER:     //订单管理

                break;
        }
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View rootView = View.inflate(context, R.layout.recyclerview_list,null);

        return new MyViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewAdapter.MyViewHolder holder, int position) {
        switch (type){
            case MyApplication.ORDER_LIST:      //订单列表
                holder.buyer_time.setVisibility(View.VISIBLE);
                break;
            case MyApplication.LOVE_LIST:     //喜欢列表
                holder.like.setVisibility(View.VISIBLE);
                holder.like.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //TODO 弹窗
                    }
                });
                break;
            case MyApplication.ADMIN_LIST:     //票务管理
                holder.delete.setVisibility(View.VISIBLE);
                holder.delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new Dialog_popWindow(context,R.style.dialog_pop_window,MyApplication.DELETE_LIST,false,String.valueOf(ticketBeanList.get(position).getId())).show();
                    }
                });
                holder.title.setText(String.valueOf(ticketBeanList.get(position).getTitle()));
                holder.price.setText(String.valueOf(ticketBeanList.get(position).getPrice()));
                Glide.with(context).load(ticketBeanList.get(position).getImgUrl()).into(holder.image);
                break;
            case MyApplication.ADMIN_ORDER:     //订单管理
                holder.buyer.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return size;
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        public final TextView delete,buyer_time,buyer,title,price;
        public final LinearLayout like;
        public final RoundedImageView image;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            delete = itemView.findViewById(R.id.delete);
            like = itemView.findViewById(R.id.like);
            buyer = itemView.findViewById(R.id.buyer);
            buyer_time = itemView.findViewById(R.id.buyer_time);
            image = itemView.findViewById(R.id.image);
            title = itemView.findViewById(R.id.title);
            price = itemView.findViewById(R.id.price);
        }
    }
}
